#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Figure 2
#####

library(here)
library(data.table)
library(ggplot2)
library(ggstance)

# source the custom functions
source("functions.R")

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}


# Fit models --------------------------------------------------------------

# main study
pca_all_main <- allCov(immPCA, chatty = F)
neighbors_all_main <- allCov(imm_neighbors2, chatty = F)
increase_all_main <- allCov(imm_increase2, chatty = F)

# repliccation
pca_all_rep <- allCov(immPCA, chatty = F, sample = "Replication")
neighbors_all_rep <- allCov(imm_neighbors2, chatty = F, sample = "Replication")
increase_all_rep <- allCov(imm_increase2, chatty = F, sample = "Replication")

# pooled
pca_all_pool <- allCov(immPCA, chatty = F, sample = "Pooled")
neighbors_all_pool <- allCov(imm_neighbors2, chatty = F, sample = "Pooled")
increase_all_pool <- allCov(imm_increase2, chatty = F, sample = "Pooled")


# Plotting preparation ----------------------------------------------------

D_names <- c("Common Humanity", "Countering Stereotypes", "Norms")

####### Combo attitudinal-behavioral results
plot_mat_rep <- data.frame(est = rep(0, 3*3), lower95 = 0, upper95 = 0, lower90 = 0, upper90 = 0, x = 0, D = NA_character_, stringsAsFactors = F, study = NA_character_)
plot_mat_rep[ 7:9, ] <- data.frame(extractPlotInfo(neighbors_all_rep, 2:4), 2, D_names, stringsAsFactors = F, study = "Replication Study (N=1,987)")
plot_mat_rep[ 4:6, ] <- data.frame(extractPlotInfo(increase_all_rep, 2:4), 1, D_names, stringsAsFactors = F, study = "Replication Study (N=1,987)")
plot_mat_rep[ 1:3, ] <- data.frame(extractPlotInfo(pca_all_rep, 2:4), 3, D_names, stringsAsFactors = F, study = "Replication Study (N=1,987)")

plot_mat_pool <- data.frame(est = rep(0, 3*3), lower95 = 0, upper95 = 0, lower90 = 0, upper90 = 0, x = 0, D = NA_character_, stringsAsFactors = F, study = NA_character_)
plot_mat_pool[ 7:9, ] <- data.frame(extractPlotInfo(neighbors_all_pool, 2:4), 2, D_names, stringsAsFactors = F, study = "Both Studies Pooled (N=4,943)")
plot_mat_pool[ 4:6, ] <- data.frame(extractPlotInfo(increase_all_pool, 2:4), 1, D_names, stringsAsFactors = F, study = "Both Studies Pooled (N=4,943)")
plot_mat_pool[ 1:3, ] <- data.frame(extractPlotInfo(pca_all_pool, 2:4), 3, D_names, stringsAsFactors = F, study = "Both Studies Pooled (N=4,943)")

plot_mat_main <- data.frame(est = rep(0, 3*3), lower95 = 0, upper95 = 0, lower90 = 0, upper90 = 0, x = 0, D = NA_character_, stringsAsFactors = F, study = NA_character_)
plot_mat_main[ 7:9, ] <- data.frame(extractPlotInfo(neighbors_all_main, 2:4), 2, D_names, stringsAsFactors = F, study = "Main Study (N=2,956)")
plot_mat_main[ 4:6, ] <- data.frame(extractPlotInfo(increase_all_main, 2:4), 1, D_names, stringsAsFactors = F, study = "Main Study (N=2,956)")
plot_mat_main[ 1:3, ] <- data.frame(extractPlotInfo(pca_all_main, 2:4), 3, D_names, stringsAsFactors = F, study = "Main Study (N=2,956)")

plotting_dat <- rbind(plot_mat_rep, plot_mat_pool, plot_mat_main)

plotting_dat$study <- factor(plotting_dat$study, levels = unique(plotting_dat$study)[c(3,1,2)])


# Plot & Save -------------------------------------------------------------

fig_2 <- ggplot(data = plotting_dat, aes(x = est, y = x, color = D, shape = D)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  ggstance::geom_linerangeh(aes(xmin = lower95, xmax = upper95), position = ggstance::position_dodgev(.5), size = .4) +
  ggstance::geom_linerangeh(aes(xmin = lower90, xmax = upper90), position = ggstance::position_dodgev(.5), size = .8) +
  geom_point(position = ggstance::position_dodgev(.5), size = 1.75, fill = "white") +
  xlab("Estimated Treatment Effect") +
  ylab(NULL) +
  scale_y_continuous(breaks = 3:1,labels = c("Immigration Index\n(SD=1)", "Welcome Immigrant\nNeighbors (1-7)", "Support Increasing\nImmigration (1-7)"), limits = c(.5, 3.5), expand = expansion(0,0)) +
  scale_color_grey(name = "Treatment:", end = 0.7, guide = guide_legend(reverse = TRUE)) +
  scale_shape_manual(values = c(15, 21,17), name = "Treatment:", guide = guide_legend(reverse = TRUE)) +
  facet_wrap( ~ study, nrow = 1) +
  theme_minimal() +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "bottom",
    strip.text = element_text(hjust = 0.5, size = 6),
    legend.text = element_text(size = 6),
    legend.title = element_text(size = 6),
    axis.text = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.margin = margin(0,0,0,0),
    legend.box.margin = margin(-10,-10,0,-10)
    )

psrm_width = 4.88284
ggsave(here("main_text", "figure_2.pdf"), fig_2, height = psrm_width*.5, width = psrm_width)

